import { getOrganizingByType } from './api';
import { Dispatch } from 'redux';
import type { IStateTypes, IOrganizingType } from './index.d';

export const MODULE_NAME: string = 'Organizing';

export const types = {
  ORGANIZING_TREE_DATA: `${MODULE_NAME}ORGANIZING_TREE_DATA`, // 列表数据
}

export default (state: IStateTypes = {
  treeData: []
}, action: any) => {
  switch (action.type) {
    case types.ORGANIZING_TREE_DATA:
      return Object.assign({}, state, {
        treeData: action.data
      })
    default: return state
  }
}

/**
 * 获取列表数据
*/
export const getOrganizing = (type: IOrganizingType = 'class') => {
  return async (dispatch: Dispatch): Promise<void> => {
    try {
      const res = await getOrganizingByType(type);
      dispatch({
        type: types.ORGANIZING_TREE_DATA,
        data: res.data
      })
    } catch { }
  }
}
